package com.goambu.driver.autenticacion.view;

import android.app.Activity;
import android.app.Fragment;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Switch;
import android.widget.TextView;

import com.goambu.driver.GoAmbuDriverConstants;
import com.goambu.driver.R;
import com.goambu.driver.autenticacion.controller.AmbulanciaController;
import com.goambu.driver.autenticacion.controller.AutenticacionController;
import com.goambu.driver.autenticacion.entities.AlertaBean;
import com.goambu.driver.autenticacion.entities.SessionBean;
import com.goambu.driver.autenticacion.entities.WorkerBean;
import com.goambu.driver.framework.view.LoadTaskDialog;
import com.goambu.driver.utils.MensajesUtility;
import com.goambu.driver.utils.MyGeocoder;

public class PerfilFragment extends Fragment {
	TextView txtNombrePersona;
	TextView txtIdConductor;
	TextView txtTelefono;
	TextView txtDireccion;
	TextView txtEmail;
	TextView txtPlaca;
	Switch switchStatus;
	Fragment fragment;
	Context context;
	boolean checked;

	WorkerBean currentworker;
	SessionBean session;
	String username = "";
	String password = "";

	public PerfilFragment() {
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		context = super.getActivity();

		View rootView = inflater.inflate(R.layout.fragment_miperfil, container,
				false);

		fragment = this;

		txtNombrePersona = (TextView) rootView
				.findViewById(R.id.txtNombrePersona);
		txtIdConductor = (TextView) rootView.findViewById(R.id.txtIdConductor);
		txtTelefono = (TextView) rootView.findViewById(R.id.txtTelefono);
		txtDireccion = (TextView) rootView.findViewById(R.id.txtDireccion);
		txtEmail = (TextView) rootView.findViewById(R.id.txtEmail);
		txtPlaca = (TextView) rootView.findViewById(R.id.txtPlaca);
		switchStatus = (Switch) rootView.findViewById(R.id.switch1);

		if (AutenticacionController.getInstance().currentUser != null) {
			if (AutenticacionController.getInstance().currentUser.worker != null) {
				if (AutenticacionController.getInstance().currentUser.worker.emergencyUnit != null) {
					currentworker = AutenticacionController.getInstance().currentUser.worker;
					txtNombrePersona.setText(currentworker.firstName + " "
							+ currentworker.lastName);
					txtIdConductor.setText("Conductor " + currentworker.nin);
					txtTelefono.setText(currentworker.mobile);
					txtEmail.setText(currentworker.email);
					txtPlaca.setText(currentworker.emergencyUnit.vehiclePlate);

					if (currentworker.emergencyUnit.currentStatus
							.equals(GoAmbuDriverConstants.STATUS_AMBULANCIA_AVAILABLE)) {
						switchStatus.setChecked(true);
					} else if (currentworker.emergencyUnit.currentStatus
							.equals(GoAmbuDriverConstants.STATUS_AMBULANCIA_INATTENTION)) {
						switchStatus.setClickable(false);
						switchStatus.setChecked(false);
					} else {
						switchStatus.setChecked(false);
					}
				}
			}
		}

		switchStatus.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				checked = isChecked;
				try {
					new MyAsyncTask(getActivity(), null).execute();
				} catch (Exception e) {
					// TODO: handle exception
				}
			}

		});
		// new LoadTaskDialog().execute();
		return rootView;
	}

	/*
	 * class MyAsyncTask extends AsyncTask<Void, Integer, Void> {
	 * 
	 * public MyAsyncTask() { }
	 * 
	 * @Override protected void onPreExecute() { }
	 * 
	 * @Override protected Void doInBackground(Void... params) { // Get the
	 * current thread's token synchronized (this) { AutenticacionController
	 * .getInstance() .getWorkerByID(
	 * AutenticacionController.getInstance().currentUser.userid); } return null;
	 * }
	 * 
	 * @Override protected void onPostExecute(Void result) {
	 * 
	 * }
	 */

	/*
	 * @Override public void onActivityCreated(Bundle savedInstanceState) { //
	 * TODO Auto-generated method stub
	 * super.onActivityCreated(savedInstanceState); if
	 * (AutenticacionController.getInstance().currentUser==null){ session =
	 * AutenticacionController.getInstance().getSession();
	 * 
	 * if (!session.username.isEmpty()) { username = session.username; password
	 * = session.password;
	 * 
	 * new LoadTaskDialog((MainActivity)getActivity(), null) .execute(); } else
	 * ((MainActivity)getActivity()).finish(); }
	 * 
	 * currentworker = AutenticacionController.getInstance().currentUser.worker;
	 * }
	 */

	class MyAsyncTask extends AsyncTask<Void, Integer, Void> {
		// Before running code in the separate thread

		private ProgressDialog pd;
		private String message;
		private Activity context;
		private boolean exito = false;

		public MyAsyncTask(Activity context, String message, ProgressDialog pd) {
			this.pd = pd;
			try {
				this.context = context;
			} catch (Exception e) {
				this.context = null;
			}

			if (message == null) {
				this.message = MensajesUtility.INFO_CARGANDO;
			} else {
				this.message = message;
			}
		}

		public MyAsyncTask(Activity context, String message) {

			this.pd = null;
			try {
				this.context = context;
			} catch (Exception e) {
				this.context = null;
			}

			if (message == null) {
				this.message = MensajesUtility.INFO_CARGANDO;
			} else {
				this.message = message;
			}
		}

		@Override
		protected void onPreExecute() {

			if (pd == null) {
				pd = new ProgressDialog(context);
			}
			pd.setCancelable(false);
			pd.setMessage(message);
			pd.show();

		}

		@Override
		protected Void doInBackground(Void... params) {
			// Get the current thread's token
			synchronized (this) {
				if (AutenticacionController.getInstance().currentUser != null) {
					if (AutenticacionController.getInstance().currentUser.worker != null) {
						if (AutenticacionController.getInstance().currentUser.worker.emergencyUnit != null) {
							if (!AutenticacionController.getInstance().currentUser.worker.emergencyUnit.currentStatus
									.equals(GoAmbuDriverConstants.STATUS_AMBULANCIA_INATTENTION)) {

								try {
									if (checked)
										exito = AmbulanciaController
												.getInstance().SetAvailable();
									else
										exito = AmbulanciaController
												.getInstance().SetUnavailable();
								} catch (Exception e) {
									// TODO: handle exception
								}

							}
						}
					}
				}
			}

			return null;

		}

		@Override
		protected void onPostExecute(Void result) {
			if (AutenticacionController.getInstance().currentUser != null) {
				if (AutenticacionController.getInstance().currentUser.worker != null) {
					if (AutenticacionController.getInstance().currentUser.worker.emergencyUnit != null) {
						if (AutenticacionController.getInstance().currentUser.worker.emergencyUnit.currentStatus
								.equals(GoAmbuDriverConstants.STATUS_AMBULANCIA_INATTENTION)) {
							switchStatus.setClickable(false);
							switchStatus.setChecked(false);
						}
					}
				}
			}

			if (pd != null && pd.isShowing()) {
				pd.dismiss();
			}
		}

	}

	/*
	 * class LoadTaskDialog extends AsyncTask<Void, Integer, Void> { private
	 * ProgressDialog pd; private String message; private Activity context;
	 * 
	 * 
	 * public LoadTaskDialog(String message) { this.pd = null;
	 * 
	 * if (message == null) { this.message = MensajesUtility.INFO_CARGANDO; }
	 * else { this.message = message; } }
	 * 
	 * @Override protected void onPreExecute() { if (pd == null) { pd = new
	 * ProgressDialog(context); } pd.setCancelable(false);
	 * pd.setMessage(message); pd.show(); }
	 * 
	 * @Override protected Void doInBackground(Void... params) { // Get the
	 * current thread's token synchronized (this) {
	 * AutenticacionController.getInstance
	 * ().getWorkerByID(AutenticacionController
	 * .getInstance().currentUser.userid); }
	 * 
	 * return null;
	 * 
	 * }
	 * 
	 * @Override protected void onPostExecute(Void result) { currentworker =
	 * AutenticacionController.getInstance().currentUser.worker; if (pd != null
	 * && pd.isShowing()) { pd.dismiss(); } }
	 * 
	 * }
	 */
}
